# @Timer:2022/3/29
# @作者:meng
# @文件名:urllib3_demo.py
import json

import urllib3

'''urllib3发送http请求'''
#1、导入import包
#2、创建poolManager
#3、调用requests请求
# def test_http():
#     pm = urllib3.PoolManager()
#     resp = pm.request(method='GET',url='http://httpbin.org/ip')
#     # print(result,type(result))
#     # print(resp.status)
#     # print(resp.headers)
#     raw = resp.data
#     # print(type(raw),raw)
#     #二进制解析成字符串
#     content = raw.decode('utf-8')
#     # print(type(content),content)
#     #json字符串解析成字典对象
#     dic1 = json.loads(content)
#     # print(type(dic1),dic1)
#     print(dic1['origin'])
# # test_http()

def http_test1():
    pm = urllib3.PoolManager()
    url = "http://httpbin.org/get"

    #定制请求头
    headers = {'school':'hogwarts'}
    #发送请求
    resp = pm.request('GET',url,headers=headers)
    # print(resp,type(resp))
    raw = json.loads(resp.data.decode('utf-8'))
    # print(type(raw))
    print(raw['headers']['School'])
http_test1()